<?php


namespace App\Admin\Controllers\Users;


use App\Models\Users;
use App\Services\SeetingStateService;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\Hash;

class UsersController extends AdminController
{
    protected $title = '用户管理';

    public function grid(){
        return Grid::make(new Users(),function (Grid $grid){
            $grid->column('nickname','姓名');
            $grid->column('mobile','手机号');
            $grid->column('user_state','用户状态')
                ->using(SeetingStateService::UserState())
                ->label([0 => 'success',1 => 'danger',2 => 'default',3 => 'primary'])
                ->setHeaderAttributes(['style' => 'color:#5b69bc']);

            $grid->disableViewButton();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('nickname','姓名');
                $filter->equal('mobile','手机号');
                $filter->equal('user_state','用户状态')->select(SeetingStateService::UserState());
            });
        });

    }

    public function form(){
        return Form::make(new Users(),function (Form $form){

            $form->text('nickname','昵称')
                ->creationRules(["unique:users"])
                ->updateRules(["unique:users,nickname,{{id}}"])->required();

            $form->tel('mobile','手机号')
                ->creationRules(['required', "unique:users"])
                ->updateRules(['required', "unique:users,mobile,{{id}}"])->required();

            $form->password('password','密码')->required();
            $form->email('email','邮箱');
            $form->radio('user_state','用户状态')
                ->options(SeetingStateService::UserState())
                ->default('0');

            $form->image('faces','头像')
                ->move('user/avatar/'.date('Y/md'))
                ->removable();

            $form->saving(function (Form $form) {
                $form->password=Hash::make($form->password);
            });

            $form->disableViewButton();
        });
    }
}
